﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CoiffeurProject.UserControls;
using CoiffeurProject.Utils;
using CoiffeurProject.DBMS;
using CoiffeurProject.DBMS.Entities;

namespace CoiffeurProject.Screens
{
    public partial class CustomerForm : Window
    {
        private static readonly double DEFAULT_WIDTH = 1920.0;

        CustTextBox name, surname, address, email, phone, description, day, month, year;
        CustCheckBox male, female;
        CloseButton close_button;
        LabeledButton save_button;
        Database db;
        Customer linked_customer;
        Image male_icon, female_icon;

        public CustomerForm(Database db)
        {
            this.db = db;

            InitializeComponent();

            Height = Height * System.Windows.SystemParameters.PrimaryScreenWidth / DEFAULT_WIDTH;
            Width = Width * System.Windows.SystemParameters.PrimaryScreenWidth / DEFAULT_WIDTH;

            InstanceTextBoxes();

            InstanceCheckBoxes();

            InstanceButtons();

            InstanceImages();

            this.Show();
        }

        private void InstanceImages()
        {
            male_icon = new Image()
            {
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/Form/male_sihlouette.png")),
                Height = 291,
                Width = 294,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(34, 102, 0, 0),
                Stretch = Stretch.Fill,
                Visibility = male.IsChecked ? Visibility.Visible : Visibility.Hidden
            };

            main_container.Children.Add(male_icon);

            female_icon = new Image()
            {
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/Form/female_sihlouette.png")),
                Height = 291,
                Width = 294,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(34, 102, 0, 0),
                Stretch = Stretch.Fill,
                Visibility = female.IsChecked ? Visibility.Visible : Visibility.Hidden
            };

            main_container.Children.Add(female_icon);
        }

        public CustomerForm(Database db, Customer customer)
            : this(db)
        {
            linked_customer = customer;

            name.ChangeText(customer.Name);
            surname.ChangeText(customer.Surname);
            phone.ChangeText(customer.Phone);
            address.ChangeText(customer.Address);
            email.ChangeText(customer.Email);
            description.ChangeText(customer.Notes);
            day.ChangeText(customer.Birthday.Day.ToString());
            month.ChangeText(customer.Birthday.Month.ToString());
            year.ChangeText(customer.Birthday.Year.ToString());

            if ((customer.Sex == "Maschio" && !male.IsChecked))
            {
                female.Deactivate();
                male.Activate();
                male_icon.Visibility = Visibility.Visible;
                female_icon.Visibility = Visibility.Hidden;
            }
            else
                if ((customer.Sex == "Femmina" && !female.IsChecked))
                {
                    male.Deactivate();
                    female.Activate();
                    male_icon.Visibility = Visibility.Hidden;
                    female_icon.Visibility = Visibility.Visible;
                }

            save_button.MakeActive();

            window_title.Content = "Modifica cliente";
        }

        private void InstanceButtons()
        {
            close_button = new CloseButton(this, new Thickness(660, 25, 0, 0), 0, 1);
            main_container.Children.Add(close_button.MainContainer);

            save_button = new LabeledButton(new Thickness(165, 730, 0, 0),
                ImagesPath.ActiveBTN, ImagesPath.UnactiveBTN, "Salva cliente", 0, 1, 220, 61, false);
            save_button.AddClickEventHandler(delegate(object s,RoutedEventArgs e) {
                var n = name.Text; var sr = surname.Text; var p = phone.Text; var sx = male.IsChecked ? "Maschio" : "Femmina";
                var em = email.Modified ? email.Text : "np"; var not = description.Modified ? description.Text : "np";
                var b = GetBirthday(); var a = address.Modified ? address.Text : "np";

                if (linked_customer == null)
                    db.Customers.Add(n, sr, p, a, sx, em, not, b);
                else
                    db.Customers.Update(linked_customer.Id, n, sr, p, a, sx, em, not, b);

                this.Close();
            });

            main_container.Children.Add(save_button.MainContainer);
        }

        private DateTime GetBirthday()
        {
            return new DateTime(int.Parse(year.Text), int.Parse(month.Text), int.Parse(day.Text));
        }

        private void InstanceCheckBoxes()
        {
            male = new CustCheckBox(new Thickness(169, 236, 0, 0), 1, 0, false);//linked_customer != null ? (linked_customer.Sex == "Maschio" ? true : false) : false);
            female = new CustCheckBox(new Thickness(327, 236, 0, 0), 1, 0, true);//linked_customer != null ? (linked_customer.Sex == "Femmina" ? true : false) : true);

            male.AddClickEventHandler(delegate(object sender, RoutedEventArgs e) { female.ToggleCheck(); female_icon.Visibility = Visibility.Hidden; male_icon.Visibility = Visibility.Visible; });
            female.AddClickEventHandler(delegate(object sender, RoutedEventArgs e) { male.ToggleCheck(); male_icon.Visibility = Visibility.Hidden; female_icon.Visibility = Visibility.Visible; });

            main_container.Children.Add(male.MainContainer);
            main_container.Children.Add(female.MainContainer);
        }

        private void InstanceTextBoxes()
        {
            name = new CustTextBox("Maria", new Thickness(167, 107, 0, 0), 0, 1);
            name.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { if (RequiredFieldsFilled() && FieldsRightlyFilled()) save_button.MakeActive(); else save_button.MakeUnactive(); });
            surname = new CustTextBox("Rossi", new Thickness(167, 165, 0, 0), 0, 1);
            surname.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { if (RequiredFieldsFilled() && FieldsRightlyFilled()) save_button.MakeActive(); else save_button.MakeUnactive(); });
            address = new CustTextBox("Via Garibaldi 10, Roma", new Thickness(167, 288, 0, 0), 0, 1);
            email = new CustTextBox("esempio@esempio.it", new Thickness(167, 348, 0, 0), 0, 1);
            phone = new CustTextBox("3301234567", new Thickness(167, 409, 0, 0), 0, 1);
            phone.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { if (RequiredFieldsFilled() && FieldsRightlyFilled()) save_button.MakeActive(); });

            day = new CustTextBox("gg", 76, 46, ImagesPath.Unactive2DigitTB, ImagesPath.Active2DigitTB,
                new Thickness(220, 474, 0, 0), 0, 1); day.TextBox.MaxLength = 2;
            day.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { CheckContraints(day, 31, 1); });
            month = new CustTextBox("mm", 76, 46, ImagesPath.Unactive2DigitTB, ImagesPath.Active2DigitTB,
                new Thickness(334, 474, 0, 0), 0, 1); month.TextBox.MaxLength = 2;
            month.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { CheckContraints(month, 31, 1); });
            year = new CustTextBox("aaaa", 107, 46, ImagesPath.Unactive4DigitTB, ImagesPath.Active4DigitTB,
                new Thickness(447, 474, 0, 0), 0, 1); year.TextBox.MaxLength = 4;
            year.AddTextChangedHandler(delegate(object s, TextChangedEventArgs e) { CheckContraints(year, DateTime.Now.Year, 0); });

            description = new CustTextBox("Allergie, particolarità del capello," + Environment.NewLine +
                "preferenze di prodotti", 495, 168, ImagesPath.UnactiveMLTB, ImagesPath.ActiveMLTB, 
                new Thickness(167, 538, 0, 0), 0, 1);

            main_container.Children.Add(name.MainContainer);
            main_container.Children.Add(surname.MainContainer);
            main_container.Children.Add(address.MainContainer);
            main_container.Children.Add(email.MainContainer);
            main_container.Children.Add(phone.MainContainer);
            main_container.Children.Add(day.MainContainer);
            main_container.Children.Add(month.MainContainer);
            main_container.Children.Add(year.MainContainer);

            main_container.Children.Add(description.MainContainer);
        }

        bool RequiredFieldsFilled()
        {
            return name.Modified && surname.Modified && phone.Modified;
        }

        bool FieldsRightlyFilled()
        {
            int v;
            return int.TryParse(day.Text, out v) && int.TryParse(month.Text, out v) && int.TryParse(year.Text, out v);
        }

        void CheckContraints(CustTextBox textBox, int upper_threshold, int lower_threshold)
        {
            int v;
            var r = int.TryParse(textBox.Text, out v);
            if (!r || v > upper_threshold || v < lower_threshold)
            {
                textBox.TextBox.Foreground = new SolidColorBrush(Colors.Red);
                save_button.MakeUnactive();
            }
            else
            {
                textBox.TextBox.Foreground = new SolidColorBrush(Colors.Black);
                if (RequiredFieldsFilled() && FieldsRightlyFilled()) save_button.MakeActive();
            }
        }
    }
}
